// <copyright file="OverlayButton.cs" company="YUIAsp.Net">
// Copyright (c) 2009 All Right Reserved
// </copyright>
// <author>raul popescu</author>
// <email>dev@yuiasp.net</email>
// <date>2009-01-30</date>
// <summary>YUIAsp.Net File</summary>

using System;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace YUIAspNet
{
    /// <summary>
    /// OverlayButton control.
    /// </summary>
    public class OverlayButton : Button
    {
        Panel _pnlOverlay;
        //Panel _pnlInner;
        ITemplate _overlayTemplate;

        /// <summary>
        /// Initializes a new instance of the <see cref="OverlayButton"/> class.
        /// </summary>
        public OverlayButton()
        {
            TypeName = "YUIAspNet.Button"; // force the type to be still a button...

            ButtonType = ButtonType.Menu;

            _pnlOverlay = new Panel();
            _pnlOverlay.ID = "menucontainer";
            _pnlOverlay.CssClass += " yui-module yui-overlay yuimenu yui-button-menu yui-menu-button-menu";
            _pnlOverlay.Style.Add("visibility", "hidden");
            //_pnlOverlay.Style.Add("display", "none");
            _pnlOverlay.Style.Add("top", "-999em");
            _pnlOverlay.Style.Add("left", "-999em");
            _pnlOverlay.Style.Add("position", "absolute");

            //_pnlInner = new Panel();
            //_pnlInner.ID = "pnlInner";
            //_pnlOverlay.Controls.Add(_pnlInner);
        }

        /// <summary>
        /// Called by the ASP.NET page framework to notify server controls that use composition-based implementation to create any child controls they contain in preparation for posting back or rendering.
        /// </summary>
        protected override void CreateChildControls()
        {
            Controls.Add(_pnlOverlay);

            if(_overlayTemplate != null)
                _overlayTemplate.InstantiateIn(_pnlOverlay);

            base.CreateChildControls();
        }

        /// <summary>
        /// Registers the menu click.
        /// </summary>
        protected override void RegisterMenuClick()
        {
            // do nothing here.. will be used in parent class
        }

        /// <summary>
        /// </summary>
        protected override void RegisterClientInitialization()
        {
            this._pnlOverlay.CssClass += " "+this.CssClass;

             //when overlay is defined, only split or menu can be defined!
            if (ButtonType != ButtonType.Menu && ButtonType != ButtonType.Split)
                ButtonType = ButtonType.Menu;

            AddProperty("MenuContainer", _pnlOverlay.ClientID);
            AddProperty("IsOverlay", true);

            base.RegisterClientInitialization();
        }

        /// <summary>
        /// Gets or sets the width of the overlay.
        /// </summary>
        /// <value>The width of the overlay.</value>
        public Unit OverlayWidth
        {
            get { return _pnlOverlay.Width; }
            set { _pnlOverlay.Width = value; }
        }

       /// <summary>
       /// Gets or sets the height of the overlay.
       /// </summary>
       /// <value>The height of the overlay.</value>
        public Unit OverlayHeight
        {
            get { return _pnlOverlay.Height; }
            set { _pnlOverlay.Height = value; }
        }

        /// <summary>
        /// Gets or sets the overlay ID that will replace the items.
        /// </summary>
        /// <value>The overlay ID.</value>
        [PersistenceMode(PersistenceMode.InnerProperty)]
        public ITemplate OverlayTemplate
        { 
            get { return _overlayTemplate; }
            set { _overlayTemplate = value; }
        }

        /// <summary>
        /// Gets or sets the button menu.
        /// </summary>
        /// <value>The button menu.</value>
        public override Menu ButtonMenu
        {
            get
            {
                throw new NotSupportedException("ButtonMenu is not supported for this control! Use either the Button class if you want to use the Button or the OverlayTemplate property instead if you want to use the BaseOverlayButton class.");
            }
            set
            {
                throw new NotSupportedException("ButtonMenu are not supported for this control! Use either the Button class if you want to use the Button or the OverlayTemplate property instead if you want to use the BaseOverlayButton class.");
            }
        }
    }
}
